package com.itqf.ipmsys.realm;

import com.itqf.ipmsys.constants.ShiroConstant;
import com.itqf.ipmsys.entity.Admin;
import com.itqf.ipmsys.entity.Permission;
import com.itqf.ipmsys.entity.Role;
import com.itqf.ipmsys.service.AdminService;
import com.itqf.ipmsys.service.PermissionService;
import com.itqf.ipmsys.service.RoleService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author corn
 * @create 2021-09-22 20:27
 * @Description: ${Description}
 */
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    /**
     * 授权信息
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Admin admin = (Admin)principals.getPrimaryPrincipal();

        if(admin == null){
            return null;
        }
        List<Role> roles = roleService.findRolesByAdminId(admin.getId());
        if (roles == null || roles.size() == 0) {
            return null;
        }
        List<Integer> roleIds = new ArrayList<>(roles.size());
        List<String> roleNames = new ArrayList<>(roles.size());
        for (Role role : roles) {
            roleIds.add(role.getId());
            roleNames.add(role.getName());
        }

        List<Permission> permissions = permissionService.findPermissionsByRoleIds(roleIds);
        List<String> permissionNames = new ArrayList<>(permissions.size());
        for (Permission permission : permissions) {
            permissionNames.add(permission.getName());
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(roleNames);
        info.addStringPermissions(permissionNames);
        return info;

        //return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String account = (String)token.getPrincipal();
        if (StringUtils.isEmpty(account)) {
            return null;
        }
        Admin admin = adminService.findAdminByAccount(account);

        if (admin == null){
            return null;
        }
        SimpleAuthenticationInfo info =
                new SimpleAuthenticationInfo(admin, admin.getPassword(), ByteSource.Util.bytes(ShiroConstant.ADMIN_SALT), ShiroConstant.REAML_NAME);
        return info;
    }
}
